package com.tools
{

	//load swf 资源
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.net.URLRequest;

	/**
	 * swf中的资源加载器
	 * @author Boder
	 * @Time 2012-2-3 10:13:58
	 */
	public class SwfResLoader
	{
		public static var temploader  :  Loader;
		public static var loadingcomplete_bool  :  Boolean;

		/**
		 * load swf
		 * @param str
		 *
		 */
		public static function loadswf(str  :  String)  :  void
		{
			temploader  =  new Loader;
			loadingcomplete_bool  =  false;
			temploader.load(new URLRequest(str));
			temploader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingcomplete);
		}

		public static function loadingcomplete(e  :  Event)  :  void
		{
			temploader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadingcomplete);
			loadingcomplete_bool  =  true;
		}

		/**
		 * 获取swf里的类，包括里面的变量和方法
		 * @param name 类名称
		 * @return
		 *
		 */
		public static function getLibClass(name  :  String)  :  Class
		{
			return (temploader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class);
		}

		/**
		 * 调用swf里的MovieClip元件
		 * @param name 对象名称
		 * @return
		 *
		 */
		public static function getLibMc(name  :  String)  :  MovieClip
		{
			var classReference  :  Class  =  getLibClass(name) as Class;
			return ((new classReference) as MovieClip);
		}

		/**
		 * 调用swf里的按钮元件
		 * @param name 对象名称
		 * @return
		 *
		 */
		public static function getLibBtn(name  :  String)  :  SimpleButton
		{
			var classReference  :  Class  =  getLibClass(name) as Class;
			return ((new classReference) as SimpleButton);
		}

		/**
		 * 调用swf里的声音
		 * @param name 对象名称
		 * @return
		 *
		 */
		public static function getLibSound(name  :  String)  :  Sound
		{
			var classReference  :  Class  =  getLibClass(name) as Class;
			return ((new classReference) as Sound);
		}

		/**
		 * 调用swf里的位图
		 * @param name 对象名称
		 * @return
		 *
		 */
		public static function getLibBitmap(name  :  String)  :  BitmapData
		{
			var classReference  :  Class  =  getLibClass(name) as Class;
			return ((new classReference(0, 0)) as BitmapData);
		}

		/**
		 * 清理
		 *
		 */
		public static function deleteloader()  :  void
		{
			temploader.unload();
			temploader  =  null;
		}
	}
}
